﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 生成充值卡视图模型
    /// </summary>
    public class GenerateRechargeCardViewModel : SubPageFrameViewModelBase
    {
        public GenerateRechargeCardViewModel() : base(PageCategory.充值管理.ToString(),Page.生成充值卡.ToString()) 
        {
            SubmitCommand = new UniversalCommand(Submit);
            CopyCommand = new UniversalCommand((param) => { 
                  if(Account!=null && Password != null)
                  {
                      try
                      {
                          Clipboard.SetText("卡号:" + Account + " 密码:" + Password);
                          this.ShowMsg("已成功复制到剪贴板");
                      }
                      catch 
                      {
                          this.ShowMsg("复制失败");
                      }
                  }
            });
        }

        string golds;
        string account;
        string password;
        Visibility isShowResult = Visibility.Collapsed;

        public UniversalCommand SubmitCommand { set; get; }

        public UniversalCommand CopyCommand { set; get; }

        public Visibility IsShowResult
        {
            get { return isShowResult; }
            set
            {
                if (this.isShowResult != value)
                {
                    this.isShowResult = value;
                    this.OnPropertyChanged("IsShowResult");
                }
            }
        }

        public string Account
        {
            get { return account; }
            set
            {
                if (this.account != value)
                {
                    this.account = value;
                    this.OnPropertyChanged("Account");
                }
            }
        }

        public string Password
        {
            get { return this.password; }
            set
            {
                if (this.password != value)
                {
                    this.password = value;
                    this.OnPropertyChanged("Password");
                }
            }
        }


        public void Submit(object param)
        {
            try
                {
                   double g= double.Parse(Golds);
                   this.ComfirmMsg("确认生成吗?", () => {
                        IsShowResult = Visibility.Collapsed;
                        AuthorServiceClient asc = new AuthorServiceClient();
                        asc.GenerateRechargeCardCompleted += this.WrapEventHandler<GenerateRechargeCardCompletedEventArgs>(asc_GenerateRechargeCardCompleted);
                        asc.GenerateRechargeCardAsync(new GenerateRechargeCardImport()
                        {
                            Golds = g,
                            Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                        });
                    });
                }
            catch
            {
                this.ShowError("请输入正确的面额");
            }
          
        }

        void asc_GenerateRechargeCardCompleted(object sender, GenerateRechargeCardCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                Account = e.Result.Info.Account;
                Password = e.Result.Info.Password;
                IsShowResult = Visibility.Visible;
            }else
            {
                this.ShowError(e.Result.Error);
            }
        }

        public string Golds 
        {
            get { return golds; }
            set
            {
                if (this.golds != value)
                {
                    this.golds = value;
                    this.OnPropertyChanged("Golds");
                }
            }
        }
    }
}
